home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471323519
Publication Date: 11/01/98

Search this book:
 
Previous Table of Contents Next


It also verifies that the aspect parameter is numeric and between 0.25 and 4. Those values allow the program to create a reasonable assortment of ellipses. It is possible that the program might want to create an ellipse with a larger or smaller aspect ratio, but that will be unlikely under most circumstances. If it does turn out to be the case, this routine can be modified to allow more extreme values. In the meantime, the values 0.25 and 4 provide reasonable error checking. If the routine receives the value 0 or 100, there is probably a bug.


Figure 5.1  Program Circles4 draws concentric circles where you click.

‘ Draw a series of concentric circles.
Private Sub DrawCircles(ByVal X As Single, ByVal Y As Single, _
    ByVal radius As Single, _
    Optional ByVal color As Variant, _
    Optional ByVal aspect As Variant)
Dim clr As Long
Dim asp As Single
Dim r As Integer

    ‘If the color is present, verify it is valid.
    If IsMissing(color) Then

        clr = ForeColor
    Else
        If Not IsNumeric(color) Then
            If DebugMode Then
                Stop
            Else
                Err.Raise err_INVALID_PARAMETER, _
                    “Circles.DrawCircles”, _
                    “The color parameter must be numeric ” & _
                    “ but it is of type ” & _
                    TypeName(color) & _
                    “ with value ” & color
            End If
        End If
        clr = CLng(color)
    End If

    ‘If the aspect is present, verify it is valid.
    If IsMissing(aspect) Then
        asp = 1#
    Else
        If Not IsNumeric(aspect) Then
            If DebugMode Then
                Stop
            Else
                Err.Raise err_INVALID_PARAMETER, _
                    “Circles.DrawCircles”, _
                    “The aspect parameter must be numeric ” & _
                    “ but it is of type ” & _
                    TypeName(aspect) & _
                    “ with value ” & aspect
            End If
        End If
        asp = CSng(aspect)
    End If

    ‘ Verify that 0.25 <= aspect <= 4.
    If (asp < 0.25) Or (asp > 4) Then
        If DebugMode Then
            Stop
        Else
            Err.Raise err_INVALID_PARAMETER, _
                “Circles.DrawCircles”, _
                “The aspect parameter must be between 0.25 and 4 ” & _
                “ but is ” & aspect
        End If
    End If

    ‘Draw the circles.
    r = radius
    Do While r >= 1
        Circle (X, Y), r, clr, , , asp
        r = r - 4
    Loop
End Sub

Use Default Values

If you use Visual Basic 5 or 6, you can use default values for arguments where a default value makes sense. You can also give more explicit data types to optional variables than variant. Default values and explicit data types let you avoid several kinds of potential errors. For example, the DrawCircles subroutine shown in the previous section no longer needs to check whether optional arguments are present. For those that are present, it no longer needs to verify that they are numeric. It can simply verify that the values it receives fall within a reasonable range.

The following code shows the DrawCircles subroutine used by program Circles5. This program is similar to program Circles4 but it uses default value and explicit typing features for optional parameters. These features are only available in Visual Basic 5 and 6, so do not try to run this program in earlier versions. This code is much more concise than the previous Circles4 code.

‘ Draw a series of concentric circles.
Private Sub DrawCircles(ByVal X As Single, ByVal Y As Single,_
    ByVal radius As Single, _
    Optional ByVal color As Long = vbBlack, _
    Optional ByVal aspect As Single = 1#)
Dim r As Integer

    ‘ Verify that 0.25 <= aspect ratio <= 4.
    If (aspect < 0.25) Or (aspect > 4) Then
        If DebugMode Then
            Stop
        Else
            Err.Raise err_INVALID_PARAMETER, _
                “Circles.DrawCircles”, _
                “The aspect parameter must be between ” & _
                “ 0.25 and 4 but is ” & aspect
        End If
    End If

    ‘ Draw the circles.
    r = radius
    Do While r >= 1
        Circle (X, Y), r, color, , , aspect
        r = r - 4
    Loop
End Sub

Note that default values must be hard coded. In Circles4, if the DrawCircles subroutine did not receive a color parameter value, it drew the circles using the form’s ForeColor. In Circles5 the default value must be defined in advance by the value vbBlack. If a program needs the kind of increased flexibility demonstrated by program Circles4, it cannot use default values.

Avoid Optional Parameters

An even better way to handle optional parameters than the previous methods is to avoid them completely. If all parameters are required, there are fewer valid ways in which the calling subroutine can invoke the routine. That makes it easier to tell if the program is calling the routine correctly.

It also makes the calling routine easier to understand even when it is correct. Which of the following makes more sense?

‘ With all parameters required:
PrintTextAt 100, 100, “Rod Stephens”, ForeColor, BackColor, Font.Size

‘ With all parameters optional:
PrintTextAt 100, 100, “Rod Stephens”, , , Font.Size

In the second subroutine call, it is not clear what the missing parameters represent and what values they will receive by default. You can make the calling routine a bit easier to understand if you require all parameters.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.